﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BuildOptimizer
{
    class BoCommandBuildEvolutionChamber : BoCommand
    {
        private const float _min = 75.0f;
        private const float _gas = 0.0f;
        private const int _time = 35;
        private const commandID _commandID = commandID.buildEvolutionChamber;

        private int _baseIndex = -1;
        public BoCommandBuildEvolutionChamber(int baseIndex)
        {
            _baseIndex = baseIndex;
        }

        public override void execute(BoGoal goal, BoDecisionNode node)
        {
            node.gameState.removeMineralsAndGas(_min, _gas);
            node.gameState.getHatcheryAtResources(_baseIndex).removeDroneFromMiningMinerals();
            node.gameState.removeUnit((int)Units.Drone);
            node.gameState.startBuildingBuilding((int)Buildings.EvolutionChamber);

            checkIfMoreRequired(goal, node);
            BoTask t = new BoTask(_time, delegate(BoDecisionNode n)
            {
                n.gameState.finishedBuildingBuilding((int)Buildings.EvolutionChamber);
                n.unlockCommand((int)commandID.buildSporeCrawler);
                n.unlockCommand((int)commandID.upgradeGroundArmor1);
                n.unlockCommand((int)commandID.upgradeMelee1);
                n.unlockCommand((int)commandID.upgradeMissile1);
            });
            node.gameState.addTask(t);
        }

        public static bool checkIfMoreRequired(BoGoal goal, BoDecisionNode node)
        {
            int nbRequired = goal.nbBuildingsRequired((int)Buildings.EvolutionChamber);
            int nbCurrently = node.gameState.nbBuildingsBeingBuilt((int)Buildings.EvolutionChamber) + node.gameState.nbBuildingsBuilt((int)Buildings.EvolutionChamber);

            if (nbRequired == -1)
            {
                if (nbCurrently < 3)
                {
                    return true;
                }
            }
            if (nbCurrently < nbRequired)
            {
                return true;
            }

            node.dismissCommand((int)commandID.buildEvolutionChamber);
            return false;
        }
        public static bool isAvailable(BoGameState gs, int baseIndex)
        {
            if (gs.getHatcheryAtResources(baseIndex).nbDronesMiningMinerals < 1)
            {
                return false;
            }
            return true;
        }
        public static bool isAffordable(BoGameState gs)
        {
            if (!gs.isResourcesAvailable(_min, _gas, 0))
            {
                return false;
            }
            return true;
        }

        public override int getCommandCode()
        {
            int code = ((int)_commandID * 100) + _baseIndex;
            return code;
        }
        public override commandID getCommandID()
        {
            return _commandID;
        }
        public override string toString()
        {
            string str = "Drone from base " + _baseIndex + " -> Evolution Chamber";
            return str;
        }
    }
}
